<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <my-table
        :tableData="orderlist"
        :titleList="titleList"
        :queryInfo="queryInfo"
        @getTableData="getOrderList"
      >
        <!-- 自定义列 -->
        <template v-slot:line>
          <el-table-column label="是否付款">
            <template slot-scope="scope">
              <el-tag v-if="!scope.row.order_pay" type="success">已付款</el-tag>
              <el-tag v-else type="danger">未付款</el-tag>
            </template>
          </el-table-column>
        </template>
        <!-- 修改订单地址按钮 -->
        <template slot="btn1" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showBox"></el-button>
        </template>
        <!-- 查看物流信息按钮 -->
        <template slot="btn2" slot-scope="scope">
          <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressBox"></el-button>
        </template>
      </my-table>
    </el-card>
    <!-- 弹窗 -->
    <el-dialog :title="dialogType?'查看物流信息':'编辑地址'" :visible.sync="dialogVisible" width="30%">
      <el-form
      v-if="!dialogType"
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <el-timeline v-else :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata.js";
import myTable from "./table.vue";

export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
        total: 0
      },
      orderlist: [],
      dialogVisible: false,
      addressForm: {
        address1: [],
        address2: ""
      },
      titleList: [
        { label: "订单号", prop: "order_number" },
        { label: "是否发货", prop: "is_send" },
        { label: "下单时间", prop: "create_time" }
      ],
      addressFormRules: {
        address1: [
          { required: true, message: "请选择省市区县", trigger: "blur" }
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "blur" }
        ]
      },
      cityData,
      progressInfo: [],
      dialogType: "" //区分弹窗的操作事件 0-编辑地址 / 1-查看物流
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$axios.get("orders", {
        params: this.queryInfo
      });

      if (res.meta.status !== 200) {
        return this.$message.error("获取订单列表失败！");
      }

      console.log(res);
      this.queryInfo.total = res.data.total;
      this.orderlist = res.data.goods;
    },
    showBox() {
      this.dialogType = 0;
      this.dialogVisible = true;
    },
    async showProgressBox() {
      const { data: res } = await this.$axios.get("/kuaidi/804909574412544580");

      if (res.meta.status !== 200) {
        return this.$message.error("获取物流进度失败！");
      }

      this.progressInfo = res.data;

      this.dialogType = 1;
      this.dialogVisible = true;
      console.log(this.progressInfo);
    }
  },
  components: {
    myTable
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
